<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class userInfoController extends Controller
{
	public function __construct()
	{
		$this ->middleware('homelogin');
	}
    public function index()
    {
        $sign = DB::table('sign')->where('uid',session('homeM')->uid)->first();
        //我的订单
        $order = DB::table('order')->where('uid',session('homeM')->uid)->get();
        //商品信息
        
            foreach ($order as $row) 
            {
            $row->oo = $row->oid;
            $row->oid = DB::table('detail')->where('oid',$row->oid)->get();
                
                foreach($row->oid as $ww)
                {
                    $ww->gid = DB::table('goods')->where('gid',$ww->gid)->first();
                }
            }
        
        
    	//个人信息
        $uid = session('homeM')->uid;
    	$data = DB::table('users')->where('uid',$uid)->first();
    	return view('home.index.userInfo',['title'=>'个人中心','data'=>$data,'order'=>$order,'sign'=>$sign]);
    }
    public function edit(Request $request)
    {
    	$this -> validate($request,[
              'uface'=>'required|image|max:2048'
         	]);
       $data = $request -> except('_token','one','two');
       $uid = session('homeM')->uid;
       $data['addr'] = $request->one.$request->two.$request->addr;
       //处理头像
        if ($request -> hasFile('uface')) 
         {
            if ($request ->file('uface')-> isValid()) 
            {
                 $extension = $request -> file('uface')->extension();
		         $filename = mt_rand(1000,9999).'.'.$extension;
		         $dir = './uploads/avatar/';
		         $request -> file('uface')->move($dir,$filename);
		         $data['uface'] = '/uploads/avatar/'.$filename;
		         //获取老用户头像
                 $oldUface = DB::table('users')->where('uid',$uid)->first()->uface;
		         if (!empty($oldUface)) 
		         {
		         	if ($oldUface != '/uploads/avatar/default.jpeg') 
		         	{
		         		if (file_exists('.'.$oldUface)) 
		         		{
		         			unlink('.'.$oldUface);
		         		}
		         	}
		         }

            }
         }else
         {
         	$data['uface'] = './uploads/avatar/default.jpg';
         }
          $res = DB::table('users')-> where('uid',$uid)->update($data);
             if ($res) 
	             {
	              return redirect('/home/index/userInfo')->with(['info'=>'修改成功']);
	             }else
	             {
	              return back() -> with(['info'=>'修改失败']);
	             }
    }
    public function delete($oid)
    {
        $res = DB::table('detail')->where('oid',$oid)->delete();
        DB::table('order')->where('oid',$oid)->delete();
        if ($res) 
        {
            return back()->with(['info'=>'删除成功']);
            //header('location:/home/index/userInfo');
        }else
        {
            return back()->with(['info'=>'删除失败']);
        }
    }
    public function status($oid)
    {
        $status = DB::table('order')->where('oid',$oid)->first();
    
        if ($status->status == 0 ) 
        {
            $data['status'] = 1;
            $res = DB::table('order')->where('oid',$oid)->update($data);
            if ($res) 
            {
                return back();
            }
        }else
        {
             return back();
        }
        
        
    }
    public function sign()
    {
      
      $sign = DB::table('sign')->where('uid',session('homeM')->uid)->first();
      if (empty($sign->num)) 
      {
         $data['uid'] = session('homeM')->uid;
         $data['ctime'] = time();
         $data['num'] = 1;
         DB::table('sign')->insert($data);
         return redirect('/home/index/userInfo');
      }else
      {
        if (date('Ymd')==date('Ymd',$sign->ctime)) 
        {
             return back()->with(['sign'=>'你今天已经签到过了']) ;
        }else
        {
             $data['ctime'] = time();
             $data['num'] = $sign->num + 1;
             DB::table('sign')->update($data);
             return back()->with(['sign'=>'签到成功']);
        }
         
      }
      
  
    }
    public function draw()
    {
        $sign = DB::table('sign') ->where('uid',session('homeM')->uid)->first()->num;
        if($sign>=20)
        {
            $a =rand(1,100);

                if($a==1)
                {
                    $a = 1;
                }elseif($a>1 && $a<=10){
                    $a = 2;
                }elseif($a>10 && $a<=40){
                    $a = 3;
                }else{
                    $a = 4;
                }
                Session::put('draw',$a);
        }else
        {
          return redirect('/home/index/userInfo')->with(['sign'=>'签到不足20天']);
        }
        return redirect('/home/index/userInfo');
    }

}